package zhr.chapter8;


import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class Example8_10 {
    public static void main(String[] args) {
        TreeMap<Integer, String> treeMap1 = new TreeMap<>();
        treeMap1.put(97, "abc79");
        treeMap1.put(123, "abc123");
        treeMap1.put(22, "abc22");
        treeMap1.put(4, "abc4");
        treeMap1.put(69, "abc69");
        treeMap1.remove(69);
        treeMap1.put(100, "xyz100");
        System.out.println(treeMap1.get(100));

        show(treeMap1.keySet());
        show(treeMap1.values());
        show(treeMap1.entrySet());
        Map.Entry<Integer, String> entry = treeMap1.firstEntry();
        System.out.println("Key: " + entry.getKey() + ",Value: " + entry.getValue());
        showMap(treeMap1);
        System.out.println();

    }

    public static void show(Collection collection) {
        Iterator it = collection.iterator();
        while (it.hasNext()) {
            System.out.print(it.next() + " ");
        }
        System.out.println();
    }

    public static void showMap(Map map) {
        Iterator it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            System.out.println("Key is " + entry.getKey() + ",Value is " + entry.getValue());
        }
        System.out.println();
    }
}
